package com.android.hamster.games;

import java.util.List;

import android.graphics.Color;

import com.android.hamster.framework.Game;
import com.android.hamster.framework.Graphics;
import com.android.hamster.framework.Input.TouchEvent;
import com.android.hamster.framework.Screen;

public class BoxGameScreen extends Screen {
	public enum GameState {
		Start, Finish
	}

	Graphics graphics;
	BoxField field = new BoxField();
	GameState state = GameState.Start;
	int areaOfChosenBox;
	float totalResultTime;

	public BoxGameScreen(Game game) {
		super(game);
		graphics = game.getGraphics();
	}

	@Override
	public void update(float deltaTime) {

		if (state == GameState.Start) {

			List<TouchEvent> touchEvents = game.getInput().getTouchEvents();
			int len = touchEvents.size();
			for (int i = 0; i < len; i++) {
				TouchEvent event = touchEvents.get(i);
				if (event.type == TouchEvent.TOUCH_UP) {
					areaOfChosenBox = field.touch(event.x, event.y);
					if (areaOfChosenBox != 0)
						state = GameState.Finish;
				}
			}
		}

		if (state == GameState.Finish) {
			totalResultTime += deltaTime;
			if (totalResultTime > 1)
				game.setScreen(new Menu(game));
		}

	}

	@Override
	public void present(float deltaTime) {

		graphics.drawBackground(Color.BLACK);

		graphics.filledRectangle(field.box1.x, field.box1.y, field.box1.width, field.box1.height, Color.BLUE);
		graphics.filledRectangle(field.box2.x, field.box2.y, field.box2.width, field.box2.height, Color.RED);
		graphics.filledRectangle(field.box3.x, field.box3.y, field.box3.width, field.box3.height,Color.YELLOW);
		graphics.filledRectangle(field.box4.x, field.box4.y, field.box4.width, field.box4.height, Color.GREEN);

		if (state == GameState.Start) {
			graphics.drawText("Click Box with largest area", 160, 40, 14, Color.GRAY);
		}

		if (state == GameState.Finish) {
			if (areaOfChosenBox == field.largestArea)
				graphics.drawText("Correct!", 160, 40, 24, Color.GREEN);
			else
				graphics.drawText("Wrong", 160, 40, 24, Color.RED);

		}

	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	@Override
	public void dispose() {
	}

}
